/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.persistence.entity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import trefliq.persistence.entity.InventoryTestSuite.*;
/**
 *
 * @author dsimmons
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({InventoryInsertTest.class, InventoryUpdateTest.class, InventoryDeleteTest.class})
public class InventoryTestSuite {
    
    public InventoryTestSuite() {
    }

    private static EntityManagerFactory emf;
    private static EntityManager em;
    private static EntityTransaction tx;
    
    @BeforeClass
    public static void setUpClass() throws Exception {
        emf = Persistence.createEntityManagerFactory("trefliqPU");
        em = emf.createEntityManager();
    }
    
    @AfterClass
    public static void tearDownClass() throws Exception {
        em.close();
        emf.close();
    }

    @Before
    public void initTransaction() {
        tx = em.getTransaction();
    }

    public static class InventoryInsertTest
    {
        @Test
        public void testInventoryInsert() {
            Inventory inventory = new Inventory();
            inventory.setMovieId(new Long(100));
            inventory.setMediaId(new Long(101));
            inventory.setItemCount(new Long(1));
            inventory.setItemsAvailable(new Long(1));
            
            tx = em.getTransaction();
            tx.begin();
            em.persist(inventory);
            tx.commit();
            assertNotNull("ID should have been generated and populated after persist", inventory.getId());        
        }
    }
    
    public static class InventoryUpdateTest {

        @Test
        public void testInventoryUpdate() {
            tx = em.getTransaction();
            tx.begin();
            Inventory inventory = em.find(Inventory.class, new Long(1));
            inventory.setItemCount(new Long(3));
            tx.commit();

            Inventory updatedInventory = em.createQuery("select i from Inventory i", Inventory.class).getSingleResult();
            assertEquals(inventory, updatedInventory);
        }
    }
    
    public static class InventoryDeleteTest {
        @Test
        public void testInventoryDelete() {
            int priorCount = em.createQuery("select o from Inventory o").getResultList().size();
            tx = em.getTransaction();
            tx.begin();
            Inventory inventory = em.find(Inventory.class, new Long(1));
            em.remove(inventory);
            tx.commit();

            int afterCount = em.createQuery("select i from Inventory i").getResultList().size();

            assertEquals(afterCount + 1, priorCount);
        }
    }
}